package cn.yfj.org.controller;

import cn.yfj.basic.config.BusinessException;
import cn.yfj.basic.util.ExcelUtil;
import cn.yfj.org.domain.ShopAuditLog;
import cn.yfj.org.vo.ShopVo;
import cn.yfj.org.service.IShopService;
import cn.yfj.org.domain.Shop;
import cn.yfj.org.query.ShopQuery;
import cn.yfj.basic.util.AjaxResult;
import cn.yfj.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;
    /*excel导出*/
    @GetMapping("/export/excel")
    public void export(HttpServletResponse response){
        //查询全部
        List<Shop> shops = shopService.queryAll();
        //导出信息设置
        ExcelUtil.exportExcel(shops,null,"入驻店铺信息",Shop.class,"shop.xls",response);
    }
    /*excel导入*/
    @PostMapping("/import/excel")
    public void importExe(@RequestPart(value = "file") MultipartFile file){
        List<Shop> shops = ExcelUtil.importExcel(file,0,1, Shop.class);
        System.out.println("导入数据一共【"+shops.size()+"】行");
        for (Shop shop : shops) {
            System.out.println(shop);
        }
    }


    //前端传值路径
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop) {
        try {
            shopService.settlement(shop);
            //执行成功返回
            return AjaxResult.me();
            //自定义异常
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");

        }
    }

    /*审核驳回*/
    @PostMapping("/audit/reject")
    public AjaxResult reject(@RequestBody ShopAuditLog shopAuditLog) {
        try {
            shopService.reject(shopAuditLog);
            return AjaxResult.me();
            //自定义业务异常
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }

    /*审核通过*/
    @PostMapping("/audit/pass")
    public AjaxResult pass(@RequestBody ShopAuditLog shopAuditLog) {
        try {
            shopService.pass(shopAuditLog);
            return AjaxResult.me();
            //自定义业务异常
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }
    /*查询数据报表支持*/
    @PostMapping("/stateCount")
    public List<Integer>stateCount(){
      List<ShopVo> shopVos = shopService.queryStateCount();
      return shopVos.stream().map(ShopVo::getCount).collect(Collectors.toList());
    }
    /**
     * 保存和修改公用的
     *
     * @param shop 传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop) {
        try {
            if (shop.getId() != null)
                shopService.update(shop);
            else
                shopService.save(shop);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！" + e.getMessage());
        }
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            shopService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！" + e.getMessage());
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public Shop get(@PathVariable("id") Long id) {
        return shopService.queryById(id);
    }


    /**
     * 查看所有的员工信息
     *
     * @return
     */
    @GetMapping()
    public List<Shop> list() {

        return shopService.queryAll();
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/list")
    public PageList<Shop> json(@RequestBody ShopQuery query) {

        return shopService.queryPage(query);
    }
}
